package com.woniu.exception.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bean.R;
import com.woniu.exception.bean.TTrack;
import com.woniu.exception.service.ITTrackService;
import com.woniu.need.bean.TDispatch;
import com.woniu.need.mapper.TDispatchMapper;
import com.woniu.order.bean.TLogistics;
import com.woniu.order.mapper.TLogisticsMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yellowDocter
 * @since 2022-08-10
 */
@RestController
@RequestMapping("/tTrack")
@Api(tags = "在途跟踪的业务")
public class TTrackController {


    @Autowired
    private TDispatchMapper dispatchMapper;

    @Autowired
    private ITTrackService trackService;

    @Autowired
    private TLogisticsMapper logisticsMapper;

    @CrossOrigin
    @GetMapping
    @ApiOperation(value = "派单信息的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public R pageTrack(Integer currentPage, Integer pageSize){

        IPage<TTrack> trackIPage = new Page(currentPage,pageSize);

        IPage<TTrack> page = trackService.page(trackIPage);

        System.out.println("page = " + page);

        List<TTrack> records = page.getRecords();

        System.out.println("records = " + records);

        //e是owner集合的一个元素，owner对象

        //通过java代码组装 house的操作
        records.forEach(e->{
            TDispatch dispatch = dispatchMapper.selectById(e.getDispatchId());
            System.out.println("dispatch = " + dispatch);
            e.setDispatch(dispatch);

        });

        records.forEach(e->{
            TLogistics logistics = logisticsMapper.selectById(e.getLogId());
            System.out.println("logistics = " + logistics);
            e.setLogistics(logistics);
        });

        long total = page.getTotal();
        System.out.println("total = " + total);

        R ok = R.OK();

        Map<String,Object> map = new HashMap<>();
        map.put("data",records);
        map.put("total",total);

        ok.setData(map);

        return ok;
    }


    @CrossOrigin
    @GetMapping("/{trackId}")
    public TTrack selectById(@PathVariable Integer trackId) {

        System.out.println("trackId = " + trackId);
        System.out.println("前端数据传进来了");

        TTrack track = trackService.getById(trackId);
        System.out.println("track = " + track);

        TLogistics logistics = logisticsMapper.selectById(track.getLogId());
        System.out.println("logistics = " + logistics);
        track.setLogistics(logistics);

        System.out.println("track = " + track);

        return track;
    }


}
